# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=pupnp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.14.6
pkgrel=2
pkgdesc="Portable SDK for UPnP* Devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/pupnp/pupnp"
license=("BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' 'staticlibs')
source=(libupnp-${pkgver}.tar.bz2::https://github.com/pupnp/pupnp/releases/download/release-${pkgver}/libupnp-${pkgver}.tar.bz2
        002-libupnp-win32.patch
        005-socket.patch
       )
sha256sums=('3168f676352e2a6e45afd6ea063721ed674c99f555394903fbd23f7f54f0a503'
            '99a0334ff2dd01fdcbe37d7cfbbbc86dacbf2cb7cccd533be3c8833e4367af1c'
            'bb8593d60e4c283b3814b87d47244b2e503d6840229cd8ec5b9d690a0c98866f')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/libupnp-${pkgver}"
  apply_patch_with_msg \
    002-libupnp-win32.patch \
    005-socket.patch

  autoreconf -fi
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  ../libupnp-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    --enable-ipv6

  make
}

package() {
  cd build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/libupnp-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
